jQuery(document).ready(function($){
	$("#featured").fadeIn("slow");
	$("#featured").tabs({ fx: { opacity: 'toggle' }}).tabs("rotate", 5000, true);
  	$('div.tabbed > div').hide();
  	$('div.more_deals').hide();
  	$('div.more_deals_local').hide();
  	$('div.tabbed1 > div').hide();
  	$('div.t1').show();
  	$('div.t4').show();
  	$('div.tabbed ul.tabs li.t1 a').addClass('tab-current');
  	$('div.tabbed1 ul.tabs li.t4 a').addClass('tab-current');
  
  	$('div.tabbed ul li a').click(function(){
		var thisClass = this.className.slice(0,2);
		$('div.tabbed > div').hide();
		$('div.more_deals').hide();
		$('div.' + thisClass).show();
		$('div.tabbed ul.tabs li a').removeClass('tab-current');
		$(this).addClass('tab-current');
	});

  	$('div.tabbed1 ul li a').click(function(){
		var thisClass = this.className.slice(0,2);
		$('div.tabbed1 > div').hide();
		$('div.more_deals_local').hide();
		$('div.' + thisClass).show();
		$('div.tabbed1 ul.tabs li a').removeClass('tab-current');
		$(this).addClass('tab-current');
	});
  	
  	/* Map Stuff */
  	var latitude;
    var longitude;
    
    function showDetails(name, link, image, tag, location){
    	$('#details').fadeIn('slow').html('<div id="deal_image"><a href="'+link+'"><img src="'+image+'" alt="'+name+'" /></a></div><div id="deal_data"><a href="'+link+'">'+name+'</a><p>'+location+'<a href="'+link+'" id="grab">GRAB</a></p></div><div class="clear"></div>');
    }
    function populateMarkers(latitude, longitude){
      
      $.ajax({url:buzzr_wp_url+'/wp-content/plugins/buzzr_ajax_map/map_query.php', dataType:'json', data:{ lat: latitude, lng: longitude, radius: 10 }, success:function(json){

        if(json.markers != 'none'){
  	      
          $.each( json.markers, function(i, m) {
  					
            $('#map_canvas').gmap('addMarker', { 'draggable': true, 'animation': google.maps.Animation.DROP, 'position': new google.maps.LatLng(m.lat, m.lng), 'tag':m.tag, 'title':m.title }, function(map,marker){  
            
              switch(m.tag)
              {
                case 'Shopping':
                  marker.setIcon('http://buzzr.in/wp-content/themes/buzzr_960/img/maps/shopping.png');
                  break;
                case 'Services':
                  marker.setIcon('http://buzzr.in/wp-content/themes/buzzr_960/img/maps/services.png');
                  break;
                case 'Restaurants & Clubs':
                  marker.setIcon('http://buzzr.in/wp-content/themes/buzzr_960/img/maps/restaurant.png');
                  break;
                case 'Fashion':
                  marker.setIcon('http://buzzr.in/wp-content/themes/buzzr_960/img/maps/fashion.png');
                  break;
                case 'Entertainment':
                  marker.setIcon('http://buzzr.in/wp-content/themes/buzzr_960/img/maps/entertainment.png');
                  break;
                case 'Flowers & Gifts':
                marker.setIcon('http://buzzr.in/wp-content/themes/buzzr_960/img/maps/gifts.png');
                break;
                case 'Health & Beauty':
                marker.setIcon('http://buzzr.in/wp-content/themes/buzzr_960/img/maps/healthbeauty.png');
                break;
                default:
                marker.setIcon('http://buzzr.in/wp-content/themes/buzzr_960/img/maps/gifts.png');
                break;
              }
              
              $(marker).click( function(event) {
            	showDetails(m.title, m.link, m.image, m.tag, m.location);
                var _this = this;
                this.setAnimation(google.maps.Animation.BOUNCE);
                setTimeout(function() { _this.setAnimation('null') }, 1500);
              });
              
  			});
          });
  				  
  			}else{
          alert('Sorry no deals in this area, please drag further!');
          $('#spinner').fadeOut('slow');
        }
  		}});
    }
    
    geocoder = new google.maps.Geocoder();
  	geocoder.geocode( { 'address': city_name+',india'}, function(results, status) {
  		var lat_lng = results[0].geometry.location;
  		var latitude = results[0].geometry.location.lat();
  		var longitude = results[0].geometry.location.lng();

  		$('#map_canvas').gmap({ 'center':lat_lng,'scrollwheel': true, 'streetViewControl': false,'mapTypeControl': false,'zoom': 10, 'callback': function(map) {

  			populateMarkers(latitude, longitude);
  		
	  		$(map).dragend( function(event) {
	  	      $('#map_canvas').gmap('clearMarkers');
	  	      $('#details').html('').fadeOut('slow');
	  	      var lat_lng = map.getCenter();
	  		    var latitude = lat_lng.lat();
	  		    var longitude = lat_lng.lng();
	  		    
	  		    populateMarkers(latitude, longitude);
	  		    
	  	    });
      
  		}});
  	});

    $("#spinner").ajaxStart(function(){
       $(this).fadeIn('slow');
    });
    
    $("#spinner").ajaxStop(function(){
      $(this).fadeOut('slow');
    });
    /* Map Stuff */
  
    
});